package com.liu;

import com.liu.common.ParentTemplate;
import com.liu.controller.ControllerTemplate;
import com.liu.entity.EntityTemplate;
import com.liu.entity.Property;
import com.liu.mapper.MapperTemplate;
import com.liu.service.ServiceTemplate;
import com.liu.utils.CommonUtils;
import com.liu.utils.MyStringUtils;
import com.liu.xml.XmlTemplate;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.create.table.Index;

import java.io.*;
import java.util.List;
import java.util.Properties;

/**
 * @author Mr.Liu
 * @version 1.0
 * @date 2024/8/4 21:14
 */
public class Main {


    public static void main(String[] args) {

        Properties props = CommonUtils.loadProperties("config.properties");

            try {
                String sql  = CommonUtils.readSqlFile(props.getProperty("sqlPath"));
                String packageName = props.getProperty("packageName");
                String funcName = props.getProperty("funcName");
//                System.out.println(sql);
                Statement statement = CCJSqlParserUtil.parse(new StringReader(sql));
                if (statement instanceof CreateTable) {
                    CreateTable createTableStatement = (CreateTable) statement;
                    ParentTemplate parentTemplate = sqlToTemplate(createTableStatement, packageName ,funcName ); //转为模板对象
                    toEntity(parentTemplate);
                    toXml(parentTemplate);
                    toMapper(parentTemplate);
                    toService(parentTemplate);
                    toController(parentTemplate);
                } else {
                    System.out.println("This is not a CREATE TABLE statement.");
                }
            } catch (JSQLParserException e) {
                e.printStackTrace();
            }

    }
    //将sql转为模板对象
    public  static ParentTemplate  sqlToTemplate(CreateTable createTableStatement,String packageName ,String funcName ){

        if(createTableStatement == null || packageName == null){
            return new ParentTemplate();
        }
        ParentTemplate parentTemplate = new ParentTemplate();
        parentTemplate.setTableName(createTableStatement.getTable().getName().replace("`" ,""));
        List<Index> indexes = createTableStatement.getIndexes(); //得到索引列
        if(indexes != null && indexes.size() != 0){
            for (Index index : indexes) {
                if(index.getType().equals("PRIMARY KEY")){
                    String columnsPrimaryKey = index.getColumns().get(0).getColumnName();
                    parentTemplate.setColumnPrimaryKey(columnsPrimaryKey.replace("`","")); //暂且只有一个为PRIMARY_KEY 的主键
                    String primaryKey = MyStringUtils.toCamelCase(columnsPrimaryKey.replace("`","") ,false);
                    parentTemplate.setPrimaryKey(primaryKey);

                }
            }
        }


        //设置生成代码啊属性
        parentTemplate.setPackageName(packageName);
        parentTemplate.setFuncName(funcName);
        List<ColumnDefinition> columnDefinitions = createTableStatement.getColumnDefinitions(); //拿到字段定义列
        String className = createTableStatement.getTable().getName().replace("`", "");
        parentTemplate.setClassName(MyStringUtils.toCamelCase(className , true)); //设置类名
        parentTemplate.setModelName(MyStringUtils.toCamelCase(className,true)+"Model");
        parentTemplate.setMapperName(MyStringUtils.toCamelCase(className,true)+"Mapper");

        for (ColumnDefinition columnDefinition : columnDefinitions) {
            Property property = new Property();
            String varName = columnDefinition.getColumnName().replace("`", "");
            property.setColumnsName(varName);
            property.setVarName(MyStringUtils.toCamelCase(varName , false)); //设置变量名
            String dataType = columnDefinition.getColDataType().getDataType();
            property.setVarType(sqlTypeToType(dataType));
            parentTemplate.getVarList().add(property);
        }
        return parentTemplate;
    }



    //将sql转为实体
    public static String toEntity(ParentTemplate parentTemplate){
        if(parentTemplate == null) return "";
        EntityTemplate entityTemplate = new EntityTemplate();
        entityTemplate.setPackageName(parentTemplate.getPackageName());
        entityTemplate.setVarList(parentTemplate.getVarList());
        entityTemplate.setClassName(parentTemplate.getClassName());
        entityTemplate.setFuncName(parentTemplate.getFuncName());
        String s = entityTemplate.toTemplate();
        System.out.println(s);
        return s;
    }

    //转为xml
    public static String toXml(ParentTemplate parentTemplate){

        if(parentTemplate == null) return "";
        XmlTemplate xmlTemplate = new XmlTemplate();
        xmlTemplate.setColumnPrimaryKey(parentTemplate.getColumnPrimaryKey());
        xmlTemplate.setPrimaryKey(parentTemplate.getPrimaryKey());
        xmlTemplate.setPackageName(parentTemplate.getPackageName());
        xmlTemplate.setMapperName(parentTemplate.getMapperName());
        xmlTemplate.setModelName(parentTemplate.getModelName());
        xmlTemplate.setVarList(parentTemplate.getVarList());
        xmlTemplate.setClassName(parentTemplate.getClassName());
        xmlTemplate.setFuncName(parentTemplate.getFuncName());
        xmlTemplate.setTableName(parentTemplate.getTableName());
        String s = xmlTemplate.toTemplate();
        System.out.println(s);
        return s;
    }

    //转mapper接口
    public static String toMapper(ParentTemplate parentTemplate){
        if(parentTemplate == null) return "";
        MapperTemplate mapperTemplate = new MapperTemplate();
        mapperTemplate.setColumnPrimaryKey(parentTemplate.getColumnPrimaryKey());
        mapperTemplate.setPrimaryKey(parentTemplate.getPrimaryKey());
        mapperTemplate.setPackageName(parentTemplate.getPackageName());
        mapperTemplate.setMapperName(parentTemplate.getMapperName());
        mapperTemplate.setModelName(parentTemplate.getModelName());
        mapperTemplate.setVarList(parentTemplate.getVarList());
        mapperTemplate.setClassName(parentTemplate.getClassName());
        mapperTemplate.setFuncName(parentTemplate.getFuncName());
        mapperTemplate.setTableName(parentTemplate.getTableName());
        String s = mapperTemplate.toTemplate();
        System.out.println(s);
        return s;
    }

    //转service
    public static String toService(ParentTemplate parentTemplate){
        if(parentTemplate == null) return "";
        ServiceTemplate serviceTemplate = new ServiceTemplate();
        serviceTemplate.setColumnPrimaryKey(parentTemplate.getColumnPrimaryKey());
        serviceTemplate.setPrimaryKey(parentTemplate.getPrimaryKey());
        serviceTemplate.setPackageName(parentTemplate.getPackageName());
        serviceTemplate.setMapperName(parentTemplate.getMapperName());
        serviceTemplate.setModelName(parentTemplate.getModelName());
        serviceTemplate.setVarList(parentTemplate.getVarList());
        serviceTemplate.setClassName(parentTemplate.getClassName());
        serviceTemplate.setFuncName(parentTemplate.getFuncName());
        serviceTemplate.setTableName(parentTemplate.getTableName());
        String s = serviceTemplate.toTemplate();
        System.out.println(s);
        return s;
    }

    //转 controller
    public static String toController(ParentTemplate parentTemplate){
        if(parentTemplate == null) return "";
        ControllerTemplate controllerTemplate = new ControllerTemplate();
        controllerTemplate.setColumnPrimaryKey(parentTemplate.getColumnPrimaryKey());
        controllerTemplate.setPrimaryKey(parentTemplate.getPrimaryKey());
        controllerTemplate.setPackageName(parentTemplate.getPackageName());
        controllerTemplate.setMapperName(parentTemplate.getMapperName());
        controllerTemplate.setModelName(parentTemplate.getModelName());
        controllerTemplate.setVarList(parentTemplate.getVarList());
        controllerTemplate.setClassName(parentTemplate.getClassName());
        controllerTemplate.setFuncName(parentTemplate.getFuncName());
        controllerTemplate.setTableName(parentTemplate.getTableName());
        String s = controllerTemplate.toTemplate();
        System.out.println(s);
        return s;
    }





    public static String sqlTypeToType(String type){
        if(type == null) return "";
        if(type.equals("bigint")) return "Long";
        if(type.equals("longtext")) return "String";
        if(type.equals("int")) return "Integer";
        if(type.equals("varchar")) return "String";
        if(type.equals("text")) return "String";
        if(type.equals("datetime")) return "Date";
        return type;
    }
}
